package br.sed.theenemy.model.dao;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import br.sed.theenemy.business.exception.DBException;
import br.sed.theenemy.model.bean.UserRequest;
import br.sed.theenemy.presentation.EnemyConstants;

/**
 * 
 * @author JulioCB
 * @email jcbjulio@gmail.com
 *
 */
public class UserRequestDao extends AbstractDao{
	
	private static DateFormat DATE_FORMAT_DB = new SimpleDateFormat(EnemyConstants.SIMPLE_DATE_FORMAT_DB);
	
	// Unica instancia da classe
	protected static UserRequestDao instanceDao;
	
	public void create(UserRequest userRequest) throws NoSuchAlgorithmException, UnsupportedEncodingException, DBException{
		String sql = "insert into `requires` (`id`, `first_name`, `last_name`, `email`, `gender`, `born_date`, `hash`, `active`, `pass`) " +
					 "values ("+getId()+"," +
					 		"'"+userRequest.getFirstName()+"'," +
					 		"'"+userRequest.getLastName()+"'," +
					 		"'"+userRequest.getEmail()+"'," +
					 		"'"+userRequest.getGender()+"'," +
					 		"'"+DATE_FORMAT_DB.format(userRequest.getBornDate())+"'," +
					 		"'"+userRequest.getHash()+"'," +
					 		"1," +					 		
					 		"'"+userRequest.getPass()+"')";		
		executeQuery(sql, "Error: " + sql);
	}

	public void invalidateOldHash(String email) throws DBException{
        String sql = "update requires set active = 0 where email = '"+ email +"'";
        
        executeQuery(sql, "Error: Updating UserRequest");
    }
	
	public UserRequest getUserRequestbyHash(String hash) throws SQLException, ParseException, DBException{
		String sql = "select * from `requires` where `hash` = '"+hash+"' and `active` = 1";
		ResultSet result = null;

		result = getRS(sql);
		if(!result.next()){
		    result.close();
	        closeConnection();
	        return null;
		}			

		UserRequest userRequest = new UserRequest();

		userRequest.setFirstName(result.getString("first_name"));
		userRequest.setLastName(result.getString("last_name"));
		userRequest.setEmail(result.getString("email"));
		userRequest.setPass(result.getString("pass"));
		userRequest.setBornDate(DATE_FORMAT_DB.parse(result.getString("born_date")));
		userRequest.setGender(result.getString("gender"));

		result.close();
		closeConnection();
		return userRequest;
	}
	
	public static UserRequestDao getInstance(){
		
		// Verifica se ja tem instancia
		if ( instanceDao == null ){			
			instanceDao = new UserRequestDao();			
		}
		
		return instanceDao;
		
	}
	
//	public static void main(String[] args) {
//		UserRequestDao usr = new UserRequestDao();
//		ResultSet rs = usr.getUserRequestbyHash("591a7863d7f466029a672d348604838");
//		try {
//			if(rs.next()){				
//				System.out.println("Meu Registro: "+rs.getString("first_name"));
//			}
//			else{			
//				System.out.println("Nada");
//			}
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//	}
}
